﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace shfa.snomtastic.systray.components
{
    public partial class TaskButton : UserControl
    {

        public Bitmap ImageUp { get; set; }
        public Bitmap ImageDown { get; set; }
        public Bitmap ImageOver { get; set; }
        public List<TextItem> TextItems {get; set;}
        


        public TaskButton(Bitmap imageUp, Bitmap imageDown, Bitmap imageOver)
        {
            this.Name = "ThreeStateButton";
            this.Size = new System.Drawing.Size(238, 50);
            this.BackColor = System.Drawing.Color.Transparent;

            this.ImageUp = imageUp;
            this.ImageDown = imageDown;
            this.ImageOver = imageOver;

            initControls();
        }


        private void initControls()
        {
            this.BackgroundImage = this.ImageUp;

            this.MouseEnter += new EventHandler(ThreeStateButton_MouseEnter);
            this.MouseDown += new MouseEventHandler(ThreeStateButton_MouseDown);
            this.MouseLeave += new EventHandler(ThreeStateButton_MouseLeave);
        }


        void ThreeStateButton_MouseHover(object sender, EventArgs e)
        {
            if (this.ImageOver != null)
            {
                this.BackgroundImage = this.ImageOver;
            }
        }

        void ThreeStateButton_MouseUp(object sender, MouseEventArgs e)
        {
            if (this.ImageOver != null)
            {
                this.BackgroundImage = this.ImageOver;
            }
        }



        void ThreeStateButton_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.ImageDown != null)
            {
                this.BackgroundImage = ImageDown;
            }
        }

        void ThreeStateButton_MouseLeave(object sender, EventArgs e)
        {
            if (this.ImageUp != null)
            {
                this.BackgroundImage = this.ImageUp;
            }

        }

        void ThreeStateButton_MouseEnter(object sender, EventArgs e)
        {
            if (this.ImageOver != null)
            {
                this.BackgroundImage = this.ImageOver;
            }
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            if (BackgroundImage != null)
            {
                Graphics offScreenGraphics;
                Bitmap offscreenBitmap;
                offscreenBitmap = new Bitmap(BackgroundImage.Width, BackgroundImage.Height);
                offScreenGraphics = Graphics.FromImage(offscreenBitmap);
                offScreenGraphics.DrawImage(BackgroundImage, 0, 0, BackgroundImage.Width, BackgroundImage.Height);
                if (TextItems!=null)
                {
                    foreach (TextItem t in TextItems)
                    {
                        DrawText(offScreenGraphics, t);
                    }
                }
                e.Graphics.DrawImage(offscreenBitmap, 0, 0);

                offScreenGraphics.Dispose();
                offscreenBitmap.Dispose();
            }
        }

        private void DrawText(Graphics g, TextItem t)
        {
           
            if (t.Text!=string.Empty)
                g.DrawString(t.Text, t.ItemFont, new SolidBrush(t.Colour), t.Location);

        }

    }
}
